package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.AdminInfo;
import com.common.Common;
import com.dao.AdminDao;
import com.utils.DESUtil;
import com.utils.PageInfo;
import com.utils.PageUtil;
import com.utils.StringUtil;

/**
 * 用户管理
 */
@WebServlet("/AdminServlet.do")
public class AdminServlet extends HttpServlet {
	AdminDao dao = new AdminDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8"); // 重点在于生成什么格式编码的数据
		String flag = request.getParameter("flag");
		if ("manage".equals(flag)) {
			manager(request, response);
		} else if ("delete".equals(flag)) {
			delete(request, response);
		} else if ("stop".equals(flag)) {
			stop(request, response);
		} else if ("deleteAll".equals(flag)) {
			deleteAll(request, response);
		} else if ("start".equals(flag)) {
			start(request, response);
		} else if ("searchForUpdate".equals(flag)) {
			searchForUpdate(request, response);
		} else if ("update".equals(flag)) {
			update(request, response);
		}else if ("pswedit".equals(flag)) {
			pswedit(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * 分页功能
	 */
	private void manager(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageSize = Common.PAGE_SIZE;
		int rowCount = dao.getAdminCount();
		int pageIndex = 1;
		String pageIndexStr = request.getParameter("pageIndex");
		if (pageIndexStr != null) {
			pageIndex = Integer.parseInt(pageIndexStr);
		}
		PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
		List<AdminInfo> adminList = dao.getAdminList(page);
		request.setAttribute("adminList", adminList);
		request.setAttribute("page", page);
		request.getRequestDispatcher("/admin/admin_manage.jsp").forward(request, response);
	}

	/*
	 * 删除用户
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		int rest = dao.deleteAdmin(idnumber);
		if (rest == 1) {
			manager(request, response);
		}
	}

	/*
	 * 批量删除用户
	 */
	private void deleteAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String[] idList = request.getParameterValues("ck_id");
		dao.delAllAdmin(idList);
		manager(request, response);

	}

	/*
	 * 锁定用户
	 */
	private void stop(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		int rest = dao.stopAdmin(idnumber);
		if (rest == 1) {
			manager(request, response);
		}
	}

	/*
	 * 解锁用户
	 */
	private void start(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		int rest = dao.startAdmin(idnumber);
		if (rest == 1) {
			manager(request, response);
		}
	}

	/**
	 * 用户修改
	 */
	private void searchForUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		AdminInfo admin = dao.getAdminInfo(idnumber);
		request.setAttribute("admin", admin);
		request.getRequestDispatcher("/admin/admin_change.jsp").forward(request, response);
	}

	/**
	 * 完成修改
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String adminName = request.getParameter("adminName");
		String note = request.getParameter("note");
		int idnumber = Integer.parseInt(id);
		AdminInfo adminInfo = new AdminInfo();
		adminInfo.setId(idnumber);
		adminInfo.setAdminName(adminName);
		adminInfo.setNote(note);
		int result = dao.updateAdmin(adminInfo);
		if (result == 1) {
			response.getWriter().print("用户角色信息更新成功");
		} else {
			response.getWriter().print("用户角色信息更新失败");
		}
	}
	
	
	/**
	 * 完成密码修改
	 */
	private void pswedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String oldpsw = request.getParameter("oldpsw");
		String psw = request.getParameter("psw");
		int idnumber = Integer.parseInt(id);
		int result = dao.updatePsw(idnumber, DESUtil.encrypt(oldpsw), DESUtil.encrypt(psw));
		if (result == 1) {
			response.getWriter().print("密码更新成功");
		} else {
			response.getWriter().print("原密码错误");
		}
	}
}
